package com.audaque.springboot.foshanupload.web.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.ArrayList;
import java.util.List;


//方式1
//@Configuration
/*@EnableWebMvc
public class WebMvcConfig extends WebMvcAutoConfiguration implements WebMvcConfigurer {*/
//方式2
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {



    @Value("${http.handleTimeout}")
    private long handleTimeout;

    //跨域:方式1
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 所有接口
                .allowedOrigins("*")
                .allowCredentials(true) // 是否发送 Cookie
                .allowedOrigins("*") // 支持域
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS","PATCH") //支持请求方式
                .allowedHeaders("*") //允许的原始请求头部信息
                .exposedHeaders("*") //暴露的头部信息
                .maxAge(handleTimeout);
    }
    //跨域:方式3
    //@Bean
    public CorsFilter corsFilter() {
        // 1.创建 CORS 配置对象
        CorsConfiguration config = new CorsConfiguration();
        // 支持域
        config.addAllowedOrigin("*");
        // 是否发送 Cookie
        config.setAllowCredentials(true);
        // 支持请求方式
        config.addAllowedMethod("*");
        // 允许的原始请求头部信息
        config.addAllowedHeader("*");
        // 暴露的头部信息
        config.addExposedHeader("*");
        config.setMaxAge(handleTimeout);
        // 2.添加地址映射
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**", config);
        // 3.返回 CorsFilter 对象
        return new CorsFilter(corsConfigurationSource);
    }

    //解决  No mapping for GET /favicon.ico 访问静态资源图标
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/META-INF/resources/");
    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        /**
         * 缀模糊匹配，默认为true
         * true: 请求/index.do,/index.abc，index.xxx都能请求到
         * false: /index。后缀也需要匹配
         */

        configurer.setUseSuffixPatternMatch(true);
        /**
         * 尾斜杠匹配
         * true：/index和/index/都可以
         * false: 只能通过/index访问
         */
        configurer.setUseTrailingSlashMatch(true);
    }




}
